package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.example.myapplication.adapter.ResultTabAdapter;
import com.example.myapplication.entity.Hots;
import com.example.myapplication.fragment.ResultAlbumFragment;
import com.example.myapplication.fragment.ResultSingerFragment;
import com.example.myapplication.fragment.ResultSongFragment;
import com.example.myapplication.fragment.ResultSongListFragment;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class ResultActivity extends AppCompatActivity {
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private List<Fragment> fragmentList = new ArrayList<>();
    private String keyword;
    private EditText edtInput;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);

        String [] titleList = {"单曲", "歌手", "专辑", "歌单"};
        tabLayout = findViewById(R.id.tab_layout);
        viewPager = findViewById(R.id.view_pager);
        edtInput = findViewById(R.id.edt_search);
        Bundle bundle = new Bundle();

        if (getIntent().getStringExtra("search_input") == null){
            Hots hots = getIntent().getParcelableExtra("search_key");
            bundle.putParcelable("search_key", hots);
            edtInput.setText(hots.getSearchWord());
        }
        else {
            keyword = getIntent().getStringExtra("search_input");
            bundle.putString("search_input", keyword);
            edtInput.setText(keyword);
        }

        edtInput.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    ((InputMethodManager) edtInput.getContext().getSystemService(
                            Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
                            ResultActivity.this.getCurrentFocus().getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);

                    String input = edtInput.getText().toString();
                    bundle.putString("input", input);
                    edtInput.setText(input);
                    return true;
                }
                return false;
            }
        });


        fragmentList.add(ResultSongFragment.newInstance(bundle));
        fragmentList.add(ResultSingerFragment.newInstance(bundle));
        fragmentList.add(ResultAlbumFragment.newInstance(bundle));
        fragmentList.add(ResultSongListFragment.newInstance(bundle));

        viewPager.setAdapter(new ResultTabAdapter(getSupportFragmentManager(), fragmentList, titleList));
        tabLayout.setupWithViewPager(viewPager);
    }
}